package LeetcodeTest_thirty_six;

//134. 加油站
public class canCompleteCircuit
{
    public static void main(String[] args)
    {
        int[] gas={5,8,2,8};
        int[] cost={6,5,6,6};
        System.out.println(solution.canCompleteCircuit(gas,cost));
    }
}
class solution
{
    public static int canCompleteCircuit(int[] gas, int[] cost)
    {
        int i,j,index,sub;
        sub=index=0;
        for (i = 0; i <gas.length; i++)
        {
            if((gas[i]-cost[i])>=0)
            {
                sub=0;
                j=i;
                while(sub>=0)
                {
                    sub+=gas[j]-cost[j];
                    j++;
                    if(j==gas.length)
                        j=0;
                    if(j==i)
                        break;
                }
                if(sub>=0)
                {
                    index = i;
                    break;
                }
            }
        }
        if(i>=gas.length)
            return -1;
        else
            return index;
    }
}